<?php
/**
 * 教练管理
 * @author xiaosong
 */
class coach extends admin{
    public function __construct(){
        parent::__construct();
    }
    
    //用户列表
    public function index(){
        $school_id=(int)$_SESSION['client']['jx_id'];
        $where='where user_type=2 and school_id='.$school_id;
        if(isset($_GET['k'])&&$_GET['k']!='') {
            $k=trim($_GET['k']);
            if(preg_match('/^1[3-9][0-9]{9}$/',$k)) $where.=' and mobile='.$k;
            else $where.=' and real_name like "%'.$k.'%"';
        }
        if(isset($_GET['type'])&&is_numeric($_GET['type'])) {
            $where.=' and kemu="'.(int)$_GET['type'].'"';
        }
        $total=db()->getOne('select count(*) from  x_user '.$where);
        if(isset($_GET['pagesize'])&&(int)$_GET['pagesize']>0) $pagesize=(int)$_GET['pagesize'];
        else $pagesize=C('PAGE_SIZE');
        if (isset($_GET['p']) && (int) $_GET['p'] > 0) $p = (int) $_GET['p'];
        else $p = 1;
        $limit=' limit '.($p-1)*$pagesize.','.$pagesize;
        $list=db()->getAll('select id,real_name,mobile,sex,money,kemu,status from x_user '.$where.' order by id DESC '.$limit);
        if(!$list) $list=array();
        $page = array('total' => $total,'pagesize' =>$pagesize,'curpage' => $p);
        $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));
    }
	
	 public function add(){
// 	    $id=(int)$_POST['id'];
//         if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'缺少参数'));
        $user['mobile']=trim($_POST['mobile']);
        if(db()->getOne('select id from x_user where mobile='.$user['mobile'])) $this->ajaxReturn(array('status'=>0,'info'=>'手机号已经被注册'));
        $id_number=trim($_POST['id_number']);
        if(strlen($id_number)<18) $this->ajaxReturn(array('status'=>0,'info'=>'身份证号格式不正确'));
        $user=get_info_by_idnum($id_number);
        $user['regtime']=NOW;
        $user['id_number']=$id_number;
        $user['real_name']=trim($_POST['real_name']);
        $user['mobile']=trim($_POST['mobile']);
        $user['nickname']='用户 '.substr_replace($user['mobile'],'****',4,8);
        $user['avatar']='';
        $user['user_type']=2;
        $user['school_id']=$_SESSION['client']['jx_id'];
        $user['kemu']=(int)$_POST['kemu'];
        $user['t_year']=(int)$_POST['t_year'];
        $user['brief']=trim($_POST['brief']);
        $insert_id=db()->insert('x_user',$user);
        if($insert_id>0){
            insertlogs('create','添加教练：'.$user['real_name'],$user,$insert_id);
            db()->insert('x_coach_extend',array('coach_id'=>$insert_id));//添加扩展记录
            if(isset($_POST['file'])&&count($_POST['file'])>0){
                foreach($_POST['file'] as $k=>$v){
                    $type=explode('_',$k,2);//$type[0]文件格式后缀；$type[1]文件类型（1：身份证，3教练证）
                    if(!isset($type[1])||!is_numeric($type[1])) break;
                    else{
                        if(empty($v)) continue;//如果文件为空，跳过
                        $a=array(1=>'身份证正面',2=>'身份证背面',3=>'教练资格证');
                        $data['uid']=$insert_id;
                        $data['title']=$a[$type[1]];
                        $data['file']=$v;
                        $data['upload_time']=NOW;
                        $data['file_type']=$type[1];
                        db()->insert('x_user_file',$data);
                    }
                }
            }
            $this->ajaxReturn(array('status'=>1,'data'=>$_POST['file']));
        }
        else $this->ajaxReturn(array('status'=>0,'info'=>db()->getError()));  
    }
    
    //编辑教练
    public function edit(){
        $id=(int)$_POST['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'缺少参数'));
        $user['mobile']=trim($_POST['mobile']);
        if(db()->getOne('select id from x_user where mobile='.$user['mobile'].' and id<>'.$id)) $this->ajaxReturn(array('status'=>0,'info'=>'手机号已经被注册'));
        $user['real_name']=trim($_POST['real_name']);
        $user['mobile']=trim($_POST['mobile']);
        $user['id_number']=trim($_POST['id_number']);
        $user['kemu']=(int)$_POST['kemu'];
        $user['t_year']=(int)$_POST['t_year'];
        $user['brief']=trim($_POST['brief']);
        $affert_num=db()->update('x_user',$user,'id='.$id);
        if($affert_num!==false){
            if($affert_num>0) insertlogs('update','更新教练信息:'.$user['real_name'],$user,$id);
            if(isset($_POST['file'])&&count($_POST['file'])>0){
                $a=array(1=>'身份证正面',2=>'身份证背面',3=>'教练资格证');
                foreach($_POST['file'] as $k=>$v){
                    $type=explode('_',$k,2);//$type[0]文件格式后缀；$type[1]文件类型（1：身份证，3教练证）
                    if(!isset($type[1])||!is_numeric($type[1])) break;
                    $file=db()->getOne('select file from x_user_file where uid='.$id.' and file_type='.$type[1].' order by id desc limit 1');
                    if($file!==false){
                        if(empty($v)){
                            db()->delete('x_user_file','uid='.$id.' and file_type='.$type[1]);
                            insertlogs('delete','删除教练('.$user['real_name'].')资料:'.$a[$type[1]],array(),$id);
                        }
                        elseif($file==$v) continue;
                        else{
                            $f['upload_time']=NOW;
                            $f['file']=$v;
                            db()->update('x_user_file',$f,'uid='.$id.' and file_type='.$type[1]);
                            insertlogs('update','更新教练('.$user['real_name'].')资料:'.$a[$type[1]],$f,$id);
                        }
                    }else{
                        $f['uid']=$id;
                        $f['title']=$a[$type[1]];
                        $f['file']=$v;
                        $f['upload_time']=NOW;
                        $f['file_type']=$type[1];
                        $insert_id=db()->insert('x_user_file',$f);
                        insertlogs('create','添加教练('.$user['real_name'].')资料:'.$a[$type[1]],$f,$insert_id);
                    }
                }
            }
            $this->ajaxReturn(array('status'=>1,'data'=>$_POST['file']));
        }
        else $this->ajaxReturn(array('status'=>0,'info'=>'修改失败'));
    }
    
    public function modify_kemu(){
        $ids=$_POST['ids'];
        $kemu=(int)$_POST['kemu'];
        if(empty($ids)) $this->ajaxReturn(array('status'=>0,'info'=>'请先选择教练'));
        $a=array(0=>'全职',2=>'科目二',3=>'科目三');
        $data['kemu']=$kemu;
        foreach($ids as $id){
            $affert_num=db()->update('x_user',$data,'id='.$id.' and user_type=2');
            if($affert_num>0){
                insertlogs('update','批量更新教练归属科目为:'.$a[$kemu],$data,$id);
            }
        }
        $this->ajaxReturn(array('status'=>1));
    }
    
    //教练详情
    public function info(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
        $info=db()->getRow('select id,real_name,id_number,mobile,sex,money,frozen_money,kemu,brief,status,regtime,lasttime,t_year from x_user where id='.$id);
        if($info){
            $file=db()->getAll('select title,file,file_type from x_user_file where uid='.$id);
            if($file) {
                $info['file']=array();
                foreach($file as $v){
                    $info['file']['img_'.$v['file_type']]=$v['file'];
                }
            }
            $this->ajaxReturn(array('status'=>1,'data'=>$info));
        }
        else{
            $this->ajaxReturn(array('status'=>0,'info'=>'没有找到该数据'));
        }
        
    }
    
    public function book_list(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数错误'));
        if(!isset($_GET['i'])) $_GET['i']=0;
        $date=date('Y-m-d',strtotime('+'.(int)$_GET['i'].' day'));
        $book_list=db()->getAll('select uid,book_kemu,book_time from x_coach_book where coach_id='.$id.' and book_date="'.$date.'"');
        $am=$pm=array();
        if($book_list){
            foreach($book_list as $v){
                $user=db()->getRow('select real_name,mobile from x_user where id='.$v['uid']);
                $v['real_name']=$user['real_name'];
                $v['mobile']=$user['mobile'];
                if($v['book_time']<12) array_push($am,$v);
                else array_push($pm,$v);
            }
            unset($book_list);
        }
        
        $this->ajaxReturn(array('status'=>1,'data'=>array('am'=>$am,'pm'=>$pm,'date'=>$date)));
    
    }
    
    public function students(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
        
        $list=db()->getAll('select id,coach_id,real_name,mobile,kemu from x_user where coach_id='.$id.' and kemu in(2,3)');
        if($list){
            foreach($list as $k=>$v){
                $res=db()->getRow('select km2_studied,km3_studied from x_student_extend where student_id='.$v['id']);
                if($v['kemu']==2) $list[$k]['kemu']='科目二('.$res['km2_studied'].'/16)';
                elseif($v['kemu']==3) $list[$k]['kemu']='科目三('.$res['km3_studied'].'/18)';
            }
        }else $list=array();
    
        $this->ajaxReturn(array('status'=>1,'data'=>$list));
    
    }
    
    public function account_log(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
        
        $account_list=db()->getAll('select * from x_user_amount_log where uid='.$id.' order by id desc');
        
        if(!$account_list) $account_list=array();
        
        $this->ajaxReturn(array('status'=>1,'data'=>$account_list));
    }
    
    public function leave_log(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
    
        $leave_list=db()->getAll('select uid,begin_time,end_time from x_leave_record where uid='.$id.' and status=1 order by id desc');
        
        if($leave_list){
            foreach($leave_list as $k=> $v){
                $user=db()->getRow('select real_name from x_user where id='.$v['uid']);
                $leave_list[$k]['real_name']=$user['real_name'];
                $diff=$v['end_time']-$v['begin_time'];
                $d=(int)($diff/86400);
                $h=(int)($diff%86400)/3600;
                $str='';
                if($d>0) $str=$d.'天';
                if($h>0) $str.=$h.'小时';
                $leave_list[$k]['duration']=$str;
            }
        }
    
        if(!$leave_list) $leave_list=array();
    
        $this->ajaxReturn(array('status'=>1,'data'=>$leave_list));
    }
    
}